#!/bin/bash
kubectl get node --no-headers=true|awk '{print $2}'|grep NotReady
flag=`echo $?`
echo "flag="$flag
if [[ $flag -eq 0 && `cat /tmp/numzz1` -eq 0 ]];then
     echo "node down"
     nodename=`kubectl get node --no-headers=true|grep NotReady|awk '{print $1}'`
     nodeip=`kubectl get node --no-headers=true -owide|grep NotReady|awk '{print $6}'`
     ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${nodename}-${nodeip} Node failure, cluster migration abnormal service. If there is any abnormality in the service access, please try again later."|mail -s Abnormal\ cluster\ node\ in\ K8S\ development\ and\ testing\ environment zhang.zhe@y3.sg,yi.he@y3.sg,kai.zhong@y3.sg,irene.ong@y3.sg,sily.zhang@ych.com,guocheng.liang@y3.sg,jiannluen.kow@y3.sg"  
     echo 1 >  /tmp/numzz1
###############################################################
     crontab -l > /tmp/temp_crontab1
     sed 's!\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!\#\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!g' /tmp/temp_crontab1 > /tmp/new_crontab1
     crontab /tmp/new_crontab1
     rm -rf /tmp/{new_crontab1,temp_crontab1}
##################################################################
elif [[ $flag -eq 1 && `cat /tmp/numzz1` -eq 1 ]];then
    echo "node up"
    ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${nodename}-${nodeip} Node failure has been restored"|mail -s Cluster\ node\ recovery\ in\ K8S\ development\ and\ testing\ environment zhang.zhe@y3.sg,yi.he@y3.sg,kai.zhong@y3.sg,sily.zhang@ych.com,irene.ong@y3.sg,guocheng.liang@y3.sg,jiannluen.kow@y3.sg"
    echo 0 >  /tmp/numzz1
###############################################################
     crontab -l > /tmp/temp_crontab1
     sed 's!\#\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!\*\/3 \* \* \* \* \/root\/script\/restartpod.sh!g' /tmp/temp_crontab1 > /tmp/new_crontab1
     crontab /tmp/new_crontab1
     rm -rf /tmp/{new_crontab1,temp_crontab1}
##################################################################
fi
